package com.lzy.cloud.payment.controller;

import com.lzy.cloud.payment.dtos.pay.AlipayPayDTO;
import com.lzy.cloud.payment.dtos.pay.WeixinPayDTO;
import com.lzy.cloud.payment.facade.PayFacade;
import com.lzy.cloud.payment.pay.weixin.WxService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @ClassName: PayController
 * @Description:该类作用域--关于第三方支付
 * @author: stone
 * @date: 2017年6月2日 下午5:22:38
 * @Copyright: 2017 www.tydic.com Inc. All rights reserved.
 */
@RestController
@Slf4j
@RequestMapping("callback")
public class PayBackController {

        Logger logger = Logger.getLogger(PayBackController.class);

        @Autowired
        private PayFacade payFacade;

        /**
         * 支付宝异步请求接口：回调
         *
         * @param alipayPayDTO
         */
        @RequestMapping(value = "/alipay/notify", method = RequestMethod.POST)
        @ResponseBody
        public void AlipayAsyNotify(AlipayPayDTO alipayPayDTO, HttpServletResponse httpServletResponse) {
            logger.info(">>>>>>>>>>>>>>>>>>>>支付宝回调开始>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            String responseContent = payFacade.payAsyNotify(alipayPayDTO);
            try {
                httpServletResponse.getWriter().append(responseContent);
            } catch (IOException e) {
                logger.info(">>>>>>>>>>>>>>>>>>>>支付宝回调异常>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                e.printStackTrace();
            }
            logger.info(">>>>>>>>>>>>>>>>>>>>支付宝回调结束>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        }

        /**
         * 微信异步请求接口：回调
         *
         * @param httpServletRequest
         * @throws IOException
         */
        @RequestMapping(value = "/wx/notify", method = RequestMethod.POST)
        @ResponseBody
        public void WxAsyNotify(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
            logger.info(">>>>>>>>>>>>>>>>>>>>微信回调开始>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            JSONObject requestJson = new WxService().processNotify(httpServletRequest);
            WeixinPayDTO weixinPayDTO = (WeixinPayDTO) JSONObject.toBean(requestJson, WeixinPayDTO.class);
            String responseContent = payFacade.payAsyNotify(weixinPayDTO);
            try {
                httpServletResponse.getWriter().append(responseContent);
            } catch (IOException e) {
                logger.info(">>>>>>>>>>>>>>>>>>>>微信回调异常>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                e.printStackTrace();
            }
            logger.info(">>>>>>>>>>>>>>>>>>>>微信回调结束>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        }

}
